/**
 * maven工具
 */
import tnx from '@/tnx.js';
import ModuleLayer from '../enums/ModuleLayer.js';

export default {
    roundXml(xml) {
        if (xml.startsWith('<project xmlns=')) {
            return xml;
        }
        return '<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n' +
            '         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">\n' +
            '    <modelVersion>4.0.0</modelVersion>\n' +
            xml +
            '</project>\n';
    },
    getProjectXml(project, indent = 0) {
        let indentPrefix = ' '.repeat(indent);
        let xml = '';
        if (project.groupId) {
            xml += indentPrefix + '<groupId>' + project.groupId + '</groupId>\n';
        }
        if (project.artifactId) {
            xml += indentPrefix + '<artifactId>' + project.artifactId + '</artifactId>\n';
        }
        if (project.version) {
            xml += indentPrefix + '<version>' + project.version + '</version>\n';
        }
        return xml;
    },
    getModulesXml(modules, indent = 0) {
        let indentPrefix = ' '.repeat(indent);
        let xml = '';
        if (modules?.length) {
            xml = indentPrefix + '<modules>\n';
            for (let module of modules) {
                xml += indentPrefix + '    <module>' + module.artifactId + '</module>\n';
            }
            xml += indentPrefix + '</modules>\n';
        }
        return xml;
    },
    getDependencyXml(dependency, indent) {
        indent = indent || 0;
        let indentPrefix = ' '.repeat(indent);
        let xml = indentPrefix + '<dependency>\n';
        xml += this.getProjectXml(dependency, indent + 4);
        if (dependency.scope) {
            xml += ' '.repeat(indent + 4) + '<scope>' + dependency.scope + '</scope>\n';
        }
        xml += indentPrefix + '</dependency>\n';
        return xml;
    },
    getDependenciesXml(dependencies, indent) {
        if (!dependencies?.length) {
            return '';
        }
        indent = indent || 0;
        let indentPrefix = ' '.repeat(indent);
        let xml = indentPrefix + '<dependencies>\n';
        for (let dependency of dependencies) {
            xml += this.getDependencyXml(dependency, indent + 4);
        }
        xml += indentPrefix + '</dependencies>\n';
        return xml;
    },
    replaceProjectVersion(xml, oldVersion, newVersion) {
        let oldVersionXml = '<version>' + oldVersion + '</version>';
        let index = xml.indexOf(oldVersionXml);
        if (index > 0) {
            let parentIndex1 = xml.indexOf('<parent>');
            if (parentIndex1 > 0) {
                let parentIndex2 = xml.indexOf('</parent>');
                if (parentIndex1 < index && index < parentIndex2) {
                    index = xml.indexOf(oldVersionXml, parentIndex2);
                }
            }
            let dependenciesIndex1 = xml.indexOf('<dependencies>');
            if (dependenciesIndex1 > 0) {
                let dependenciesIndex2 = xml.indexOf('</dependencies>');
                if (dependenciesIndex1 < index && index < dependenciesIndex2) {
                    index = xml.indexOf(oldVersionXml, dependenciesIndex2);
                }
            }
            if (index > 0) {
                return xml.substring(0, index) + '<version>' + newVersion + '</version>'
                    + xml.substring(index + oldVersionXml.length);
            }
        }
        return xml;
    },
    replaceParentVersion(xml, module, newVersion) {
        let parentIndex = xml.indexOf('<parent>');
        if (parentIndex > 0) {
            let parentXml = xml.substring(parentIndex, xml.indexOf('</parent>', parentIndex) + '</parent>'.length);
            let parent = tnx.util.xml.parse(parentXml).parent;
            if (module.groupId.startsWith(parent.groupId) && module.version === parent.version) {
                let oldVersionXml = '<version>' + module.version + '</version>';
                let index = xml.indexOf(oldVersionXml, parentIndex);
                if (index > 0) {
                    return xml.substring(0, index) + '<version>' + newVersion + '</version>'
                        + xml.substring(index + oldVersionXml.length);
                }
            }
        }
        return xml;
    },
}
